package com.fili.gui;

import java.awt.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import com.fili.params.Param;
import com.fili.params.ListParam;
import com.fili.JUscator;
import com.fili.SharedData;

/**
 * Date: Nov 12, 2005
 * Time: 4:15:22 PM
 *
 * @author Claudiu-Florin FILIMON for Borux Consulting S.R.L.
 */
public class ComponentPositionSaver implements ComponentListener
{
    private Component component;
    private String componentName;

    private Param p;
/*-------------------------------------------------------------------------------------
    Getters and setters
 --------------------------------------------------------------------------------------*/

/*-------------------------------------------------------------------------------------
	Constructors
 --------------------------------------------------------------------------------------*/

    public ComponentPositionSaver(Component component, String componentName, Rectangle defaultBounds)
    {
        componentName += ".compPosition";

        this.component = component;
        this.componentName = componentName;

        if (SharedData.getPEngine().getParam(componentName) == null)
        {
            component.setBounds(defaultBounds);
        }

        p = SharedData.getPEngine().addParam(new Param(componentName, new ListParam(new Integer[]{component.getX(), component.getY(), component.getWidth(), component.getHeight()})));

        Integer[] data = ((ListParam) (p.getCurrentValue())).getItems();

        if (data[0]<0)
            data[0] = 0;
        if (data[1]<0)
            data[1] = 0;
        component.setBounds(data[0], data[1], data[2], data[3]);
        component.addComponentListener(this);
    }
/*-------------------------------------------------------------------------------------
    Methods
 --------------------------------------------------------------------------------------*/

/*-------------------------------------------------------------------------------------
    Overrides
 --------------------------------------------------------------------------------------*/

/*-------------------------------------------------------------------------------------
    Implementations
 --------------------------------------------------------------------------------------*/

    public void componentResized(ComponentEvent e)
    {
        p.setCurrentValue(new ListParam(new Integer[]{component.getX(), component.getY(), component.getWidth(), component.getHeight()}));
    }

    public void componentMoved(ComponentEvent e)
    {
        p.setCurrentValue(new ListParam(new Integer[]{component.getX(), component.getY(), component.getWidth(), component.getHeight()}));
    }

    public void componentShown(ComponentEvent e)
    {
    }

    public void componentHidden(ComponentEvent e)
    {
    }
}
